package com.example.restatr.Atcivity.LawyerConsultation;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.LawyerConsultation.Bean.LegalAdviceBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MyLegalAdviceAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private ListView lv;
    private LinearLayout Null;
    private TextView nullText;
    private LinearLayout loading;
    private List<LegalAdviceBean.RowsBean> rowsBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_legaladvice_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        lv = findViewById(R.id.lv);
        Null = findViewById(R.id.Null);
        nullText = findViewById(R.id.null_text);
        loading = findViewById(R.id.loading);
        nullText.setText("暂无咨询...");

        Tools.SetBarTitle(toolbar, barTitle, "我的咨询", search, this, false);
        GetLegalAdvice();
    }

    private void GetLegalAdvice() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/lawyer-consultation/legal-advice/list", MainActivity.sp.getString("token", ""));
            LegalAdviceBean legalAdviceBean = new Gson().fromJson(token, LegalAdviceBean.class);
            rowsBeans.addAll(legalAdviceBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    loading.setVisibility(View.GONE);
                    lv.setAdapter(new adapter());
                } else {
                    loading.setVisibility(View.GONE);
                    Null.setVisibility(View.VISIBLE);
                }
            });
        }).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowsBeans.size();
        }

        @Override
        public Object getItem(int position) {
            return rowsBeans.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("Range")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.service_info_item, null);
            TextView title = convertView.findViewById(R.id.title);
            TextView undertaker = convertView.findViewById(R.id.undertaker);
            TextView name = convertView.findViewById(R.id.name);
            TextView date = convertView.findViewById(R.id.date);
            TextView content = convertView.findViewById(R.id.content);
            ImageView img = convertView.findViewById(R.id.img);
            CardView reply = convertView.findViewById(R.id.reply);
            TextView replyContent = convertView.findViewById(R.id.replyContent);
            CardView comment = convertView.findViewById(R.id.comment);

            String state = "";
            String textColor = "";
            title.setText("用户" + rowsBeans.get(position).getFromUserId() + "▶" + rowsBeans.get(position).getLawyerName());
            switch (rowsBeans.get(position).getState()) {
                case "0":
                    state = "未受理";
                    textColor = "#FD4D5D";
                    break;
                case "1":
                    state = "受理中";
                    textColor = "#00B7FF";
                    break;
                case "2":
                    state = "已完成";
                    textColor = "#388E3C";
                    if (rowsBeans.get(position).getEvaluate() == null) {
                        comment.setVisibility(View.VISIBLE);
                    }
                    break;
            }
            undertaker.setText(state);
            undertaker.setTextColor(Color.parseColor(textColor));
            if (rowsBeans.get(position).getLegalExpertiseName() != null) {
                name.setText(rowsBeans.get(position).getLegalExpertiseName());
            } else {
                name.setVisibility(View.GONE);
            }
            date.setText(rowsBeans.get(position).getCreateTime());
            content.setText(rowsBeans.get(position).getContent());
            Glide.with(MyLegalAdviceAt.this)
                    .load(Tools.ip + rowsBeans.get(position).getImageUrls())
                    .transform(new RoundedCorners(15))
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Drawable> target, boolean b) {
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable drawable, Object o, Target<Drawable> target, DataSource dataSource, boolean b) {
                            img.setImageDrawable(drawable);
                            img.setVisibility(View.VISIBLE);
                            return true;
                        }
                    })
                    .into(img);
            comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MyLegalAdviceAt.this, LawyerInfoAt.class);
                    intent.putExtra("intent", "comment");
                    intent.putExtra("data", getIntent().getStringExtra("data"));
                    intent.putExtra("rows", new Gson().toJson(rowsBeans.get(position)));
                    startActivity(intent);
                }
            });
            return convertView;
        }
    }

}